package xworker.swt.dataObject.bind.values;

import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.dataObject.DataObject;
import xworker.swt.dataObject.bind.Value;

import java.text.DecimalFormat;

public class NumberFormater  implements Value {
    Thing thing;
    DecimalFormat sf;

    public NumberFormater(Thing thing, String pattern){
        this.thing = thing;
        sf = new DecimalFormat(pattern);
    }

    @Override
    public Object getValue(Object value, DataObject dataObject, ActionContext actionContext) {
        String attributeName = thing.doAction("getAttributeName", actionContext);
        if(attributeName != null && !attributeName.isEmpty()){
            value = dataObject.get(attributeName);
        }

        if(value != null) {
            return sf.format(value);
        }else{
            return null;
        }
    }

    public static NumberFormater create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String pattern = self.doAction("getPattern", actionContext);
        return new NumberFormater(self, pattern);
    }
}
